var DataTables = function () {

    bootbox.setDefaults({
        'locale':'zh_CN'
    });

    return {
        userTable:function () {
            $('#userTable').DataTable({
                serverSide:true,
                paginate:true,
                processing:true,
                ordering:false,
                autoWidth:true,
                language: {
                    url:'/js/biz/Chinese.json'
                },
                order: [[1, 'asc']],
                lengthMenu: [
                    [5, 10, 15, 20],
                    [5, 10, 15, 20] // change per page values here
                ],
                //默认数据条数
                pageLength: 5,
                columns:[{
                    data:"id",
                    visible:false
                },{
                    data:"username",
                    title:"用户名"
                },{
                    data:"sex",
                    title:"性别"
                },{
                    data:"phone",
                    title:"电话"
                },{
                    data:"idCard",
                    title:"身份证"
                },{
                    data:"address",
                    title:"地址"
                },{
                    data:"status",
                    title:"状态",
                    render:function (data) {
                        if(data.status ==0){
                            return '禁用'
                        }else{
                            return '启用'
                        }
                    }
                },{
                    data:"description",
                    title:"描述"
                },{
                    data: null,
                    title:'操作',
                    width:'23%'
                }],
                ajax:  {
                    url:"/api/user/findByUsernameLike",
                    type:"post",
                    data:function (dataSet) {
                        var param = {};
                        param.draw = dataSet.draw;
                        param.start = dataSet.start;
                        param.length = dataSet.length;
                        param.search=dataSet.search.value;
                        return param;
                    }
                },
                //增加的按钮
                columnDefs: [
                    {
                        targets: -1,
                        render:function(data){
                            return "<div class='btn-group'>"+
                                "<button class='btn btn-default' onclick='User.restPwd("+data.id+")'><i class='glyphicon glyphicon-refresh'>密码重置</i></button>"+
                                "<button class='btn btn-default' onclick='User.getUser("+data.id+")'><i class='glyphicon glyphicon-edit'>编辑</i></button>"+
                                "<button class='btn btn-default' onclick='User.deleteUser("+data.id+")'><i class='glyphicon glyphicon-remove'>删除</i></button>"+
                                "</div>"
                        }
                    }
                ]

            });

        },
        roleTable:function () {
            $('#roleTable').DataTable({
                serverSide:true,
                paginate:true,
                processing:true,
                ordering:false,
                autoWidth:true,
                language: {
                    url:'/js/biz/Chinese.json'
                },
                order: [[1, 'asc']],
                lengthMenu: [
                    [5, 10, 15, 20],
                    [5, 10, 15, 20] // change per page values here
                ],
                pageLength: 5,
                columns:[{
                    data:"id",
                    visible:false
                },{
                    data:"name",
                    title:"角色"
                },{
                    data:"status",
                    title:"状态",
                    render:function (data) {
                        if(data.status ==0){
                            return '禁用'
                        }else{
                            return '启用'
                        }
                    }
                },{
                    data:"description",
                    title:"描述"
                },{
                    data: null,
                    title:'操作',
                    width:'15%'
                }],
                ajax:  {
                    url:"/api/role/findRoleByLike",
                    type:"post",
                    data:function (dataSet) {
                        var param = {};
                        param.draw = dataSet.draw;
                        param.start = dataSet.start;
                        param.length = dataSet.length;
                        param.search=dataSet.search.value;
                        return param;
                    }
                },
                columnDefs: [
                    {
                        targets: -1,
                        render:function(data){
                            return "<div class='btn-group'>"+
                                "<button class='btn btn-default' onclick='Role.getRole("+data.id+")'><i class='glyphicon glyphicon-edit'>编辑</i></button>"+
                                "<button class='btn btn-default' onclick='Role.deleteRole("+data.id+")'><i class='glyphicon glyphicon-remove'>删除</i></button>"+
                                "</div>"
                        }
                    }
                ]

            });
        },
        groupTable:function () {
            var table=$('#groupTable').DataTable({
                serverSide:true,
                paginate:true,
                processing:true,
                ordering:false,
                autoWidth:true,
                searching:false,
                language: {
                    url:'/js/biz/Chinese.json'
                },
                order: [[1, 'asc']],
                lengthMenu: [
                    [5, 10, 15, 20],
                    [5, 10, 15, 20] // change per page values here
                ],
                pageLength: 5,
                columns:[{
                    data:"id",
                    visible:false
                },{
                    data:"groupName",
                    title:"权限组"
                },{
                    data:"description",
                    title:"描述"
                },{
                    data: null,
                    title:'操作',
                    width:'35%'
                }],
                ajax:  {
                    url:"/api/group/findAll",
                    type:"post",
                    data:function (dataSet) {
                        var param = {};
                        param.draw = dataSet.draw;
                        param.start = dataSet.start;
                        param.length = dataSet.length;
                        param.search=dataSet.search.value;
                        return param;
                    }
                },
                columnDefs: [
                    {
                        targets: -1,
                        render:function(data){
                            return "<div class='btn-group'>"+
                                "<button class='btn btn-default' onclick='PermissionGroup.getGroup("+data.id+")'><i class='glyphicon glyphicon-edit'>编辑</i></button>"+
                                "<button class='btn btn-default' onclick='PermissionGroup.delete("+data.id+")'><i class='glyphicon glyphicon-remove'>删除</i></button>"+
                                "</div>"
                        }
                    }
                ]

            });

            $('#groupTable tbody').on( 'click', 'tr', function () {

                var data = table.row( this ).data();
                var permissionTree = $.fn.zTree.getZTreeObj("permissionTree");
                var menuTree = $.fn.zTree.getZTreeObj("menuTree");

                if ( $(this).hasClass('selected') ) {
                    $(this).removeClass('selected');
                } else {
                    table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                }
                if($(this).hasClass('selected')){
                    menuTree.checkAllNodes(false);
                    menuTree.cancelSelectedNode();

                    permissionTree.checkAllNodes(false);
                    permissionTree.cancelSelectedNode();
                    $.ajax({
                        url: "/api/permissionGroup/findBySysGroupId",
                        data:{id:data.id},
                        type:'GET',
                        success: function (data) {
                            if(data.code =='401'){
                                bootbox.alert(data.message);
                            }else{
                                if (data.data !=undefined && data.data.length>0) {
                                    for (var i = 0; i < data.data.length; i++) {
                                        if(data.data[i].type =='m'){
                                            var node=menuTree.getNodeByParam("id", data.data[i].sysPermissionId);
                                            if(node){
                                                menuTree.checkNode(node);
                                                menuTree.selectNode(node,true);
                                            }
                                        }else{
                                            var node = permissionTree.getNodeByParam("id", data.data[i].sysPermissionId);
                                            if(node){
                                                permissionTree.checkNode(node);
                                                permissionTree.selectNode(node,true);
                                            }
                                        }
                                    }
                                }else{
                                    permissionTree.checkAllNodes(false);
                                    permissionTree.cancelSelectedNode();

                                    menuTree.checkAllNodes(false);
                                    menuTree.cancelSelectedNode();
                                }
                            }
                        }
                    });
                }
            } );

        }
    }
}();